<template>
  <div class="register-page">
    <van-nav-bar title="面经注册" />
    <div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[
            { required: true, message: '请填写用户名' },
            {
              pattern: /^[a-zA-Z]{6,12}$/,
              message: '用户名必须为字母或数字组合，长度6-20',
            },
          ]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[
            { required: true, message: '请填写密码' },
            {
              pattern: /^[a-zA-Z]{6,12}$/,
              message: '用户名必须为字母或数字组合，长度6-20',
            },
          ]"
        />
        <div style="margin: 16px">
          <van-button type="primary" block native-type="submit">注册</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'RegisterPage',
  props: {},
  components: {},
  data () {
    return {
      username: '',
      password: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async  onSubmit () {
      try {
        // 发送请求 成功执行上面
        // register.username = this.username
        await register({
          username: this.username,
          password: this.password

        })
        // console.log(this.username)
        // console.log(this.password)
        this.$router.push('/login')
      } catch (error) {
        // 失败执行下面
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
